package com.extweb4j.web.controller;

import com.extweb4j.core.anno.AuthAnno;
import com.extweb4j.core.anno.Log;
import com.extweb4j.core.bean.PageInfo;
import com.extweb4j.core.controller.ExtController;
import com.extweb4j.core.model.ExtCompete;
import com.extweb4j.core.model.ExtNews;
import com.extweb4j.core.model.ExtTender;
import com.extweb4j.web.bean.Compete;
import com.extweb4j.web.bean.Tender;
import com.extweb4j.web.utils.DateUtils;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZhaoWeinan
 * @date 2018/12/16
 * @description
 */
public class CompeteController extends ExtController {


    /**
     * 新增
     */
    @Log("新建询价单")
    @AuthAnno
    @Before(Tx.class)
    public void addXunJia(){
        //ExtUser user = getExtModel(ExtUser.class);
        ExtCompete compete = new ExtCompete();
        Map<String, String[]> parasMap = getRequest().getParameterMap();
        if (parasMap.get("title") != null && parasMap.get("title").length > 0){
            compete.set("title",parasMap.get("title")[0]);
        }

        if (parasMap.get("tenderno") != null && parasMap.get("tenderno").length > 0){
            compete.set("tenderno",parasMap.get("tenderno")[0]);
        }

        if (parasMap.get("tenderarea") != null && parasMap.get("tenderarea").length > 0){
            compete.set("tenderarea",parasMap.get("tenderarea")[0]);
        }

        if (parasMap.get("noticetype") != null && parasMap.get("noticetype").length > 0){
            compete.set("noticetype",parasMap.get("noticetype")[0]);
        }

        if (parasMap.get("source") != null && parasMap.get("source").length > 0){
            compete.set("source",parasMap.get("source")[0]);
        }

        if (parasMap.get("tenderman") != null && parasMap.get("tenderman").length > 0){
            compete.set("tenderman",parasMap.get("tenderman")[0]);
        }

        String[] section = parasMap.get("section");

        if (section != null && section.length > 0){
            compete.set("section", StringUtils.join(section,","));
        }

        String[] purchasetype = parasMap.get("purchasetype");

        if (purchasetype != null && purchasetype.length > 0){
            compete.set("purchasetype", StringUtils.join(purchasetype,","));
        }

        if(parasMap.get("opentime") != null && parasMap.get("opentime").length > 0){
            try {
                compete.set("opentime", new Timestamp(DateUtils.getDate(parasMap.get("opentime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(parasMap.get("endtime") != null && parasMap.get("endtime").length > 0){
            try {
                compete.set("endtime", new Timestamp(DateUtils.getDate(parasMap.get("endtime")[0]).getTime()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (parasMap.get("noticecontent") != null && parasMap.get("noticecontent").length > 0){
            compete.set("noticecontent",parasMap.get("noticecontent")[0]);
        }


        compete.set("createtime",new Timestamp(System.currentTimeMillis()));
        compete.save();
        success();
    }

    /**
     * 列表
     */
    @AuthAnno
    public void xunJiaList(){
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");
        Page<ExtCompete> pageData = ExtCompete.dao.pageXunJia(page,limit,keywords);
        renderJson(pageData);
    }

    /**
     * 列表
     */
    @AuthAnno
    public void jinJiaList(){
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");
        Page<ExtCompete> pageData = ExtCompete.dao.pageJinJia(page,limit,keywords);
        renderJson(pageData);
    }


    /**
     * 列表
     */
    @AuthAnno
    public void taiPanList(){
        int page = getParaToInt("page");
        int limit = getParaToInt("limit");
        String keywords = getPara("keywords");
        Page<ExtCompete> pageData = ExtCompete.dao.pageTaiPan(page,limit,keywords);
        renderJson(pageData);
    }

}
